package top.mengchaob.fileserver.netty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import top.mengchaob.deploy.api.DeployServiceI;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @title：
 * @author: mengchaob
 * @date: 2021年05月12日 11:22
 * @description:
 */
@Component
public class NettyRunnerImpl implements ApplicationRunner {
    /**
     * 核心线程池大小
     */
    public static final int CORE_POOL_SIZE = 5;
    /**
     * 最大线程池大小
     */
    public static final int MAX_POOL_SIZE = 10;
    /**
     * 阻塞任务队列大小
     */
    public static final int QUEUE_CAPACITY = 100;
    /**
     * 空闲线程存活时间
     */
    public static final Long KEEP_ALIVE_TIME = 1L;
    Logger logger = LoggerFactory.getLogger(NettyRunnerImpl.class);
    @Override
    public void run(ApplicationArguments args) throws Exception {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy());
        try {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    logger.debug("开始启动文件服务器");
                    try {
                        HttpFileStart.start(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    logger.debug("文件服务器已经启动");
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            //关闭线程池
            executor.shutdown();
        }
    }
}
